<?php

namespace App\Http\Controllers\Api;

use App\Models\Batch;
use App\Models\Talent;
use App\Transformers\BatchTransformer;
use App\Transformers\TalentTransfromer;
use Illuminate\Http\Request;

class BatchController extends Controller
{
    public function index(Request $request, Batch $batch)
    {
        $batch = $batch->newQuery();
        $batch->where('type', (int) $request->input('type'));
        if ($request->input('name')) {
            $batch->where('name', $request->input('name'));
        }
        if ($request->input('batch_date')) {
            $batch->where('batch_date', $request->input('batch_date'));
        }
        if ($request->input('group')) {
            $batch->where('group', $request->input('group'));
        }
        if ($request->input('type')) {
            $batch->where('type', $request->input('type'));
        }
        $batch->where('time', '>=', strtotime('-1 week'));
        $batch->orderBy('batch_datetime', 'ASC');
        $batchs = $batch->paginate(20);

//        $batch_ids = [];
//        foreach ($batchs as $batch) {
//            $batch_ids[] = $batch->id;
//        }
//        switch ($request->input('type')) {
//            case 1:
//                $talents = Talent::where('interview_status', 2)->whereIn('first_batch_id', $batch_ids)->get(['first_batch_id', 'recommend']);
//                $posts = [];
//                foreach ($talents as $talent) {
//                    $posts[] = $talent->recommend;
//                }
//                break;
//            case 2:
//                $talents = Talent::where('interview_status', 6)->whereIn('second_batch_id', $batch_ids)->get();
//                break;
//            case 3:
//                $talents = Talent::where('interview_status', 10)->whereIn('hr_batch_id', $batch_ids)->get();
//        }
//        echo json_encode($talents);

        return $this->response->paginator($batchs, new BatchTransformer());
    }

    public function show(Batch $batch)
    {
        return $this->response->item($batch, new BatchTransformer());
    }

    public function change_interviewer(Request $request, Batch $batch)
    {
        $batch->update([
            'interviewer' => $request->input('interviewer'),
        ]);
        return $this->response->item($batch, new BatchTransformer());
    }

    public function interviewList(Batch $batch, Request $request)
    {
        $type = $request->input('type');
        $talent = null;
        switch ($type) {
            case 'first':
                $talent = Talent::where('first_batch_id', $batch->id)
                    ->where('interview_status', 2)->get();
                break;
            case 'second':
                $talent = Talent::where('second_batch_id', $batch->id)
                    ->where('interview_status', 6)->get();
                break;
            case 'hr':
                $talent = Talent::where('hr_batch_id', $batch->id)
                    ->where('interview_status', 10)->get();
                break;
        }

        return $this->response->collection($talent, new TalentTransfromer());
    }

    public function tiaoji_list(Batch $batch, Request $request)
    {
        $batch_datetime = $batch->batch_datetime;
        $type = 1;
        switch ($request->input('lun')) {
            case 'first':
                $type = 1;
                break;
            case 'second':
                $type = 2;
                break;
            case 'hr':
                $type = 3;
                break;
        }
        $batchs = Batch::where('batch_datetime', $batch_datetime)->where('type', $type)->get();
        return $this->response->collection($batchs, new BatchTransformer());
    }

    public function tiaoji(Request $request)
    {
        $talent = Talent::find($request->input('id'));
        switch ($request->input('lun')) {
            case 'first':
                $talent->update([
                    'first_batch_id' => $request->input('batch_id')
                ]);
                break;
            case 'second':
                $talent->update([
                    'second_batch_id' => $request->input('batch_id')
                ]);
                break;
            case 'hr':
                $talent->update([
                    'hr_batch_id' => $request->input('batch_id')
                ]);
                break;
        }
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function searchByName(Request $request)
    {
        switch ($request->input('type')) {
            case 'first':
                $batchs = Batch::where('name', 'like', '%'.$request->input('name').'%')->where('type', 1)->get();
                return $this->response->collection($batchs, new BatchTransformer());
                break;
            case 'second':
                $batchs = Batch::where('name', 'like', '%'.$request->input('name').'%')->where('type', 2)->get();
                return $this->response->collection($batchs, new BatchTransformer());
                break;
            case 'hr':
                $batchs = Batch::where('name', 'like', '%'.$request->input('name').'%')->where('type', 3)->get();
                return $this->response->collection($batchs, new BatchTransformer());
                break;
        }
    }
}
